﻿namespace EasyWeb.Test
{
    using System.ComponentModel.Composition;
    using System.Linq;
    using System.Threading.Tasks;
    using EasyWeb.Configuration;
    using EasyWeb.Data;
    using EasyWeb.Extensions;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class EWEntityDefinition_Test : BaseTest
    {
        [Import(typeof(IEWConfigurationDao))]
        public IEWConfigurationDao ConfigurationDao;

        [Import(typeof(IEWEntityDefinitionDao))]
        public IEWEntityDefinitionDao EntityDefintionDao;

        [Import(typeof(IEWFieldDefinitionDao))]
        public IEWFieldDefinitionDao FieldDefintionDao;

        [TestMethod]
        public async Task All_Test()
        {
            await Create("test1", new EWFieldDefinitionBoolean(), new EWFieldDefinitionChoice());
            await Create("test2", new EWFieldDefinitionCurrency());
            await Create("test3");

            var entities = await this.EntityDefintionDao.SelectAsync();

            var test1 = entities.SingleOrDefault(e => e.InternalName == "test1");
            Assert.IsNotNull(test1);
            Assert.IsNotNull(test1.FieldDefinitions.SingleOrDefault(f => f.GetType() == typeof(EWFieldDefinitionBoolean)));
            Assert.IsNotNull(test1.FieldDefinitions.SingleOrDefault(f => f.GetType() == typeof(EWFieldDefinitionChoice)));

            var test2 = entities.SingleOrDefault(e => e.InternalName == "test2");
            Assert.IsNotNull(test2);
            Assert.IsNotNull(test2.FieldDefinitions.SingleOrDefault(f => f.GetType() == typeof(EWFieldDefinitionCurrency)));

            var test3 = entities.SingleOrDefault(e => e.InternalName == "test3");
            Assert.IsNotNull(test3);
            Assert.AreEqual(0, test3.FieldDefinitions.Count);
        }

        [TestInitialize]
        public override void TestInitialize()
        {
            base.TestInitialize();

            EWExtensionManager.CompositionContainer.SatisfyImportsOnce(this);

            TestInitializeSystemPrefix();
        }

        private void TestInitializeSystemPrefix()
        {
            var config = EWHelpers.Async.RunSync(async () => await this.ConfigurationDao.SelectAsync<EWSystemPrefixConfiguration>());
            config.Value = "testcase_";
            EWHelpers.Async.RunSync(async () => await this.ConfigurationDao.UpdateAsync(config));
        }

        private async Task Create(string name, params EWFieldDefinition[] fields)
        {
            var entity = new EWEntityDefinition()
            {
                InternalName = name,
                DisplayName = CreateText(),
                Description = CreateText(),
            };

            await this.EntityDefintionDao.CreateAsync(entity);

            if (fields != null)
            {
                for (int i = 0; i < fields.Length; i++)
                {
                    var field = fields[i];
                    field.InternalName = string.Format("InternalName{0}", i);
                    field.EntityDefinitionId = entity.Id;
                    await this.FieldDefintionDao.CreateAsync(field);
                }
            }
        }

        private static EWText CreateText()
        {
            var text = new EWText();
            text.Values.Add(new EWTextValue("en", "v1"));
            text.Values.Add(new EWTextValue("zh-Hans", "v2"));

            return text;
        }
    }
}
